package com.android.fragmentdemo.roomdb
import java.util.concurrent.Executors

object RoomDBUserUtil {
    private val executorService by lazy {
        Executors.newSingleThreadExecutor()
    }

    fun insertUser(vararg user: User) {
        executorService.execute {
            RoomDBEntrance.getRoomDataBase().userDao().insertUser(*user)
        }
    }

    fun queryAll(): MutableList<User> {
        return RoomDBEntrance.getRoomDataBase().userDao().queryAll()
    }

    fun update(user: User) {
        executorService.execute {
            RoomDBEntrance.getRoomDataBase().userDao().update(user)
        }
    }

    fun update(id: String, hobbies: MutableList<String>) {
        executorService.execute {
            val user = RoomDBEntrance.getRoomDataBase().userDao().query(id)
            user.hobbies = hobbies
            RoomDBEntrance.getRoomDataBase().userDao().update(user)
        }
    }

    fun update2(id: String, petList: MutableList<Pet>) {
        executorService.execute {
            val user = RoomDBEntrance.getRoomDataBase().userDao().query(id)
            user.petList = petList
            RoomDBEntrance.getRoomDataBase().userDao().update(user)
        }
    }

    fun delete(user: User) {
        executorService.execute {
            RoomDBEntrance.getRoomDataBase().userDao().delete(user)
        }
    }
}